{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# genHtml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fileName:fmt.txt\n"
     ]
    }
   ],
   "source": [
    "fileName = input(\"fileName:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "modelName:hbfx\n"
     ]
    }
   ],
   "source": [
    "modelName = input(\"modelName:\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "Input = \"1\"\n",
    "Select = \"2\"\n",
    "Date = \"3\"\n",
    "Money = \"4\"\n",
    "Rate = \"5\"\n",
    "Area = \"6\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# base input html tmpl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "class HtmlTmpl:\n",
    "    def __init__(self):\n",
    "        self.headTmpl = \"\"\"<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%>\n",
    "<script type=\"text/javascript\">\n",
    "\n",
    "</script>\n",
    "<div class=\"view\" id=\"form_{}\">\n",
    "    <input type=\"hidden\" id=\"\" name=\"\" value=\"\" />\n",
    "    <fieldset>\n",
    "        <legend></legend>\n",
    "        <table class=\"tableStyle\" formMode=\"transparent\" >\n",
    "\"\"\"\n",
    "        self.footTmpl = \"\"\"\n",
    "        </table>\n",
    "    </fieldset>\n",
    "</div>\n",
    "\"\"\"\n",
    "        self.inputTmpl = \"\"\"<input type=\"text\" id=\"{}\" name=\"{}\" \n",
    "                        value='<s:property value=\"{}\"/>' \n",
    "                        class=\"input_w180 validate[{}]\" />\"\"\"\n",
    "        self.selectTmpl = \"\"\"<select id=\"{}\" name=\"{}\" \n",
    "                        selectedValue='<s:property value=\"{}\"/>' \n",
    "                        prompt='<s:text name=\"label.select.default.value\" />'\n",
    "                        class=\"validate[{}]\" selWidth=\"180\" />\"\"\"\n",
    "        self.dateTmpl = \"\"\"<input type=\"text\" id=\"{}\"  name=\"{}\" \n",
    "                        value='<s:date name=\"{}\"  format=\"{}\"/>'\n",
    "                        class=\"date input_w180 validate[{}]\" />\"\"\"\n",
    "        self.moneyTmpl = \"\"\"<input type=\"text\" id=\"{}\" name=\"{}\"\n",
    "                        value='<s:property value=\"{}\"/>' intLength=\"12\"\n",
    "                        class=\"money input_w180_right validate[custom[illegalLetter]] validate[{}]\" />\"\"\"\n",
    "        self.rateTmpl = \"\"\"<input type=\"text\" id=\"{}\" name=\"{}\" \n",
    "                        value='<s:property value=\"{}\"/>' \n",
    "                        class=\"input_w180_right validate[custom[moneyRate_2_4]] validate[{}]\"\n",
    "                        onblur=\"replenish4(this)\" />\"\"\"\n",
    "        self.areaTmpl = \"\"\"<textarea style=\"width: 690px;\" id=\"{}\" name=\"{}\" class=\"data validate[{}]\" maxNum=\"200\" >${}</textarea>\"\"\"\n",
    "    #model为实体类名字 传入的arr格式为 [propName,propType,required]\n",
    "    def genTmpl(self,model,arr):\n",
    "        if len(arr) != 3:\n",
    "            print(\"文件结束或输入文件格式错误!\")\n",
    "            return \"<span></span>\"\n",
    "        propName = arr[0]\n",
    "        propType = arr[1]\n",
    "        required = arr[2]\n",
    "        \n",
    "        id = model + \"_\" + propName #id = model_propName\n",
    "        name = model + \".\" + propName #name = model.propName\n",
    "        value = model + \".\" + propName #value = {model.propName}\n",
    "        if propType == Input:\n",
    "            if required == \"0\":\n",
    "                return self.inputTmpl.format(id,name,value,\"\")\n",
    "            else:\n",
    "                return self.inputTmpl.format(id,name,value,\"required\")\n",
    "        elif propType == Select:\n",
    "            if required == \"0\":\n",
    "                return self.selectTmpl.format(id,name,value,\"\")\n",
    "            else:\n",
    "                return self.selectTmpl.format(id,name,value,\"required\")\n",
    "        elif propType == Date:\n",
    "            if required == \"0\":\n",
    "                return self.dateTmpl.format(id,name,value,\"%{getText('date.format')}\",\"\")\n",
    "            else:\n",
    "                return self.dateTmpl.format(id,name,value,\"%{getText('date.format')}\" ,\"required\")\n",
    "        elif propType == Money:\n",
    "            if required == \"0\":\n",
    "                return self.moneyTmpl.format(id,name,value,\"\")\n",
    "            else:\n",
    "                return self.moneyTmpl.format(id,name,value,\"required\")\n",
    "        elif propType == Rate:\n",
    "            if required == \"0\":\n",
    "                return self.rateTmpl.format(id,name,value,\"\")\n",
    "            else:\n",
    "                return self.rateTmpl.format(id,name,value,\"required\")\n",
    "        elif propType == Area:\n",
    "            if required == \"0\":\n",
    "                return self.areaTmpl.format(id,name,\"\",\"{\" + value + \"}\")\n",
    "            else:\n",
    "                return self.areaTmpl.format(id,name,\"required\",\"{\" + value + \"}\")\n",
    "        else:\n",
    "            print(\"Unknown Type!\")\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# list tmpl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ListTmpl:\n",
    "    def __init__(self):\n",
    "        self.genlTmpl = \"\"\"{}\n",
    "    display: '{}',\n",
    "    name: '{}',\n",
    "    align: 'left',\n",
    "    width: 100\n",
    "{},\"\"\"\n",
    "        self.moneyTmpl = \"\"\"{}\n",
    "    display: '{}',\n",
    "    name: '{}',\n",
    "    align: 'right',\n",
    "    width: 100,\n",
    "    render: function(rowdata, rowindex, value, column){}\n",
    "        if( value != '' && value != null){}\n",
    "            return formatRound(value,2);\n",
    "        {}\n",
    "    {}\n",
    "{},\"\"\"\n",
    "        self.rateTmpl = \"\"\"{}\n",
    "    display: '{}',\n",
    "    name: '{}',\n",
    "    align: 'right',\n",
    "    width: 100,\n",
    "    render: function(rowdata, rowindex, value, column){}\n",
    "        if( value != '' && value != null){}\n",
    "            return formatRound(value,2);\n",
    "        {}\n",
    "    {}\n",
    "{},\"\"\"\n",
    "        self.dateTmpl = \"\"\"{}\n",
    "    display: '{}',\n",
    "    name: '{}',\n",
    "    align: 'right',\n",
    "    width: 100,\n",
    "    render: function(rowdata, rowindex, value, column){}\n",
    "        if( value != '' && value != null){}\n",
    "            return value.substring(0,10);\n",
    "        {}\n",
    "    {}\n",
    "{},\"\"\"\n",
    "        pass\n",
    "    def genTmpl(self,cnName,arr):\n",
    "        if len(arr) < 2:\n",
    "            print(arr,\"格式错误!\")\n",
    "            return\n",
    "        enName = arr[0]\n",
    "        propType = arr[1]\n",
    "        if propType == Date:\n",
    "            return self.dateTmpl.format(\"{\",cnName,enName,\"{\",\"{\",\"}\",\"}\",\"}\")\n",
    "        elif propType == Money:\n",
    "            return self.moneyTmpl.format(\"{\",cnName,enName,\"{\",\"{\",\"}\",\"}\",\"}\")\n",
    "        elif propType == Rate:\n",
    "            return self.rateTmpl.format(\"{\",cnName,enName,\"{\",\"{\",\"}\",\"}\",\"}\")\n",
    "        else:\n",
    "            return self.genlTmpl.format(\"{\",cnName,enName,\"}\")\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# generate list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "f1 = open(fileName,\"r\",encoding='utf-8') #input file with format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "flist = open(modelName + \".list\",\"a+\",encoding='utf-8') #generate list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "listTmpl = ListTmpl()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "for line in f1:\n",
    "    cnName,arr = line.split(':',1)\n",
    "    flist.write(listTmpl.genTmpl(cnName.strip(),arr.split()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "f1.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "flist.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# generate base input html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "f1 = open(fileName,\"r\",encoding='utf-8') #input file with format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "f2 = open(modelName + \".jsp\",\"a+\",encoding='utf-8') #generate input jsp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmpl = HtmlTmpl()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "299"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f2.write(tmpl.headTmpl.format(modelName))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "tr = \"\"\"            <tr>\n",
    "                {0}\n",
    "                {1}\n",
    "            </tr>\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "td = \"\"\"<td {2}>\n",
    "                    {0}\n",
    "                </td>\n",
    "                <td {3}>\n",
    "                    {1}\n",
    "                </td>\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "for line1 in f1:\n",
    "    prop1,fmt1 = line1.split(':',1)\n",
    "    td1Input = tmpl.genTmpl(modelName,fmt1.split())\n",
    "    if Area == fmt1.split()[1]:\n",
    "        td1 = td.format(prop1,td1Input,'width=\"15%\"','colspan=\"5\" width=\"90%\"')\n",
    "    else:\n",
    "        td1 = td.format(prop1,td1Input,'','')\n",
    "    try:\n",
    "        line2 = f1.readline()\n",
    "        prop2,fmt2 = line2.split(':',1)\n",
    "        td2Input = tmpl.genTmpl(modelName,fmt2.split())\n",
    "        if Area == fmt2.split()[1]:\n",
    "            td2 = td.format(prop2,td2Input,'width=\"15%\"','colspan=\"5\" width=\"90%\"')\n",
    "        else:\n",
    "            td2 = td.format(prop2,td2Input,'','')\n",
    "    except (ValueError,IndexError):\n",
    "        td2 = \"\" #最后一个节点可能为空\n",
    "    finally:\n",
    "        f2.write(tr.format(td1,td2))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "41"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f2.write(tmpl.footTmpl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success Creat JSP file: qykhxx.jsp\n"
     ]
    }
   ],
   "source": [
    "print(\"Success Creat JSP file:\",modelName + \".jsp\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "f2.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "f1.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
